home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / zcomposite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  10.5 KB  |  438 lines

  1.  
  2. /* zcomposite.c - by Tom McReynolds, SGI */
  3.  
  4. /* Compositing images that include depth information. */
  5.  
  6. /* To use this program, you select a region of the "room" scene by
  7.    rubber-banding the region with the left mouse button.  Once a
  8.    region is selected, position the region with the middle mouse
  9.    button.  The region will be composited using the region image and
  10.    the region's depth information.  With the right mouse button, you
  11.    can shift the region's depth values nearer or further away. */
  12.  
  13. #include <stdlib.h>
  14. #include <GL/glut.h>
  15. #include <stdio.h>
  16.  
  17. /* Create a single component texture map */
  18. GLfloat *
  19. make_texture(int maxs, int maxt)
  20. {
  21.   int s, t;
  22.   static GLfloat *texture;
  23.  
  24.   texture = (GLfloat *) malloc(maxs * maxt * sizeof(GLfloat));
  25.   for (t = 0; t < maxt; t++) {
  26.     for (s = 0; s < maxs; s++) {
  27.       texture[s + maxs * t] = ((s >> 4) & 0x1) ^ ((t >> 4) & 0x1);
  28.     }
  29.   }
  30.   return texture;
  31. }
  32.  
  33. GLboolean stencil = GL_TRUE;
  34.  
  35. /* ARGSUSED1 */
  36. void 
  37. key(unsigned char key, int x, int y)
  38. {
  39.   switch (key) {
  40.   case 't':            /* toggle using stencil */
  41.     if (stencil == GL_TRUE)
  42.       stencil = GL_FALSE;
  43.     else
  44.       stencil = GL_TRUE;
  45.     glutPostRedisplay();
  46.     break;
  47.   case '\033':
  48.     exit(0);
  49.     break;
  50.   }
  51. }
  52.  
  53. enum {
  54.   SPHERE = 1, CONE
  55. };
  56. enum {
  57.   X, Y, Z
  58. };
  59.  
  60. int startx, starty;
  61. int wid, ht;
  62. int oldwid = 0, oldht = 0;
  63.  
  64. const int WINDIM = 512;
  65. const GLfloat FRUSTDIM = 110.f;
  66. const GLfloat FRUSTNEAR = 320.f;
  67. const GLfloat FRUSTFAR = 540.f;
  68. const GLfloat FRUSTDIFF = 540.f - 320.f;
  69.  
  70. GLboolean drawmode = GL_FALSE;
  71. GLboolean depthmode = GL_FALSE;
  72. GLboolean rubberbandmode = GL_FALSE;
  73. GLfloat *color;
  74. GLfloat *depth;
  75. GLfloat depthbias = 0.f;
  76. GLfloat raspos[] =
  77. {0.f, 0.f, -430.f};
  78.  
  79. int winWidth = 512;
  80. int winHeight = 512;
  81.  
  82. GLfloat sx = 0;
  83. GLfloat sy = 0;
  84.  
  85. /* Overlay Stuff */
  86. int transparent;
  87. int red;
  88. int overlaySupport = 0;
  89.  
  90. void
  91. setRasterPosXY(int x, int y)
  92. {
  93.   raspos[X] = (x - winWidth / 2) * sx;
  94.   raspos[Y] = (y - winHeight / 2) * sy;
  95.  
  96.   glRasterPos3fv(raspos);
  97.  
  98.   glutPostRedisplay();
  99. }
  100.  
  101. void
  102. setRasterPosZ(int y)
  103. {
  104.   raspos[Z] = -(FRUSTNEAR + y * FRUSTDIFF / winHeight);
  105.  
  106.   depthbias = (y - winHeight / 2.f) / winHeight;
  107.  
  108.   glRasterPos3fv(raspos);
  109.  
  110.   glutPostRedisplay();
  111. }
  112.  
  113. void
  114. motion(int x, int y)
  115. {
  116.   y = winHeight - y;
  117.   if (drawmode)
  118.     setRasterPosXY(x, y);
  119.  
  120.   if (rubberbandmode) {
  121.     wid = x - startx;
  122.     ht = y - starty;
  123.     if (overlaySupport) glutPostOverlayRedisplay();
  124.   }
  125.   if (depthmode)
  126.     setRasterPosZ(y);
  127. }
  128.  
  129. /* redraw function for overlay: used to show selected region */
  130. void
  131. overlay(void)
  132. {
  133.   if (glutLayerGet(GLUT_OVERLAY_DAMAGED)) {
  134.     glClear(GL_COLOR_BUFFER_BIT);
  135.   } else {
  136.     glIndexi(transparent);
  137.     glBegin(GL_LINE_LOOP);
  138.     glVertex2i(startx, starty);
  139.     glVertex2i(startx + oldwid, starty);
  140.     glVertex2i(startx + oldwid, starty + oldht);
  141.     glVertex2i(startx, starty + oldht);
  142.     glEnd();
  143.   }
  144.  
  145.   glIndexi(red);
  146.   glBegin(GL_LINE_LOOP);
  147.   glVertex2i(startx, starty);
  148.   glVertex2i(startx + wid, starty);
  149.   glVertex2i(startx + wid, starty + ht);
  150.   glVertex2i(startx, starty + ht);
  151.   glEnd();
  152.  
  153.   oldwid = wid;
  154.   oldht = ht;
  155.  
  156.   glFlush();
  157. }
  158.  
  159. /* used to get current width and height of viewport */
  160. void
  161. reshape(int wid, int ht)
  162. {
  163.   if (overlaySupport) {
  164.     glutUseLayer(GLUT_OVERLAY);
  165.     glMatrixMode(GL_PROJECTION);
  166.     glLoadIdentity();
  167.     gluOrtho2D(0, wid, 0, ht);  /* 1 to 1 with window */
  168.     glMatrixMode(GL_MODELVIEW);
  169.     glViewport(0, 0, wid, ht);
  170.   }
  171.  
  172.   glutUseLayer(GLUT_NORMAL);
  173.   glViewport((GLint) (-wid * .1), (GLint) (-ht * .1),
  174.     (GLuint) (wid * 1.2), (GLuint) (ht * 1.2));
  175.  
  176.   winWidth = wid;
  177.   winHeight = ht;
  178.  
  179.   sx = 2 * FRUSTDIM / (winWidth * 1.2);
  180.   sy = 2 * FRUSTDIM / (winHeight * 1.2);
  181. }
  182.  
  183. void
  184. mouse(int button, int state, int x, int y)
  185. {
  186.   y = winHeight - y;    /* flip y orientation */
  187.   if (state == GLUT_DOWN)
  188.     switch (button) {
  189.     case GLUT_LEFT_BUTTON:  /* select an image */
  190.       startx = x;
  191.       starty = y;
  192.       wid = 0;
  193.       ht = 0;
  194.       rubberbandmode = GL_TRUE;
  195.       if (overlaySupport) glutShowOverlay();
  196.       break;
  197.     case GLUT_MIDDLE_BUTTON:
  198.       glutUseLayer(GLUT_NORMAL);
  199.       if (color && depth) {
  200.         drawmode = GL_TRUE;
  201.         setRasterPosXY(x, y);
  202.       }
  203.       break;
  204.     case GLUT_RIGHT_BUTTON:  /* change depth */
  205.       glutUseLayer(GLUT_NORMAL);
  206.       if (color && depth) {
  207.         depthmode = GL_TRUE;
  208.         setRasterPosZ(y);
  209.       }
  210.       break;
  211.   } else                /* GLUT_UP */
  212.     switch (button) {
  213.     case GLUT_LEFT_BUTTON:
  214.       rubberbandmode = GL_FALSE;
  215.       if (overlaySupport) glutHideOverlay();
  216.       wid = x - startx;
  217.       ht = y - starty;
  218.       if (wid < 0) {
  219.         wid = -wid;
  220.         startx = x;
  221.       }
  222.       if (ht < 0) {
  223.         ht = -ht;
  224.         starty = y;
  225.       }
  226.       color = (GLfloat *) realloc(color, wid * ht * 3 * sizeof(GLfloat));
  227.       depth = (GLfloat *) realloc(depth, wid * ht * sizeof(GLfloat));
  228.  
  229.       glutUseLayer(GLUT_NORMAL);
  230.       glReadPixels(startx, starty, wid, ht, GL_RGB, GL_FLOAT, color);
  231.       glReadPixels(startx, starty, wid, ht, GL_DEPTH_COMPONENT, GL_FLOAT,
  232.         depth);
  233.       break;
  234.     case GLUT_MIDDLE_BUTTON:
  235.       drawmode = GL_FALSE;
  236.       break;
  237.     case GLUT_RIGHT_BUTTON:  /* change depth */
  238.       depthmode = GL_FALSE;
  239.       break;
  240.     }
  241. }
  242.  
  243. /* Called when window needs to be redrawn */
  244. void
  245. redraw(void)
  246. {
  247.   /* material properties for objects in scene */
  248.   static GLfloat wall_mat[] =
  249.   {1.f, 1.f, 1.f, 1.f};
  250.  
  251.   glutUseLayer(GLUT_NORMAL);
  252.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  253.  
  254.   /* Note: wall verticies are ordered so they are all front facing this lets
  255.      me do back face culling to speed things up. */
  256.  
  257.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
  258.  
  259.   /* floor */
  260.   /* make the floor textured */
  261.   glEnable(GL_TEXTURE_2D);
  262.  
  263.   /* Since we want to turn texturing on for floor only, we have to make floor 
  264.      a separate glBegin()/glEnd() sequence. You can't turn texturing on and
  265.      off between begin and end calls */
  266.   glBegin(GL_QUADS);
  267.   glNormal3f(0.f, 1.f, 0.f);
  268.   glTexCoord2i(0, 0);
  269.   glVertex3f(-100.f, -100.f, -320.f);
  270.   glTexCoord2i(1, 0);
  271.   glVertex3f(100.f, -100.f, -320.f);
  272.   glTexCoord2i(1, 1);
  273.   glVertex3f(100.f, -100.f, -520.f);
  274.   glTexCoord2i(0, 1);
  275.   glVertex3f(-100.f, -100.f, -520.f);
  276.   glEnd();
  277.  
  278.   glDisable(GL_TEXTURE_2D);
  279.  
  280.   /* walls */
  281.  
  282.   glBegin(GL_QUADS);
  283.   /* left wall */
  284.   glNormal3f(1.f, 0.f, 0.f);
  285.   glVertex3f(-100.f, -100.f, -320.f);
  286.   glVertex3f(-100.f, -100.f, -520.f);
  287.   glVertex3f(-100.f, 100.f, -520.f);
  288.   glVertex3f(-100.f, 100.f, -320.f);
  289.  
  290.   /* right wall */
  291.   glNormal3f(-1.f, 0.f, 0.f);
  292.   glVertex3f(100.f, -100.f, -320.f);
  293.   glVertex3f(100.f, 100.f, -320.f);
  294.   glVertex3f(100.f, 100.f, -520.f);
  295.   glVertex3f(100.f, -100.f, -520.f);
  296.  
  297.   /* ceiling */
  298.   glNormal3f(0.f, -1.f, 0.f);
  299.   glVertex3f(-100.f, 100.f, -320.f);
  300.   glVertex3f(-100.f, 100.f, -520.f);
  301.   glVertex3f(100.f, 100.f, -520.f);
  302.   glVertex3f(100.f, 100.f, -320.f);
  303.  
  304.   /* back wall */
  305.   glNormal3f(0.f, 0.f, 1.f);
  306.   glVertex3f(-100.f, -100.f, -520.f);
  307.   glVertex3f(100.f, -100.f, -520.f);
  308.   glVertex3f(100.f, 100.f, -520.f);
  309.   glVertex3f(-100.f, 100.f, -520.f);
  310.   glEnd();
  311.  
  312.   glPushMatrix();
  313.   glTranslatef(-80.f, -80.f, -420.f);
  314.   glCallList(SPHERE);
  315.   glPopMatrix();
  316.  
  317.   glPushMatrix();
  318.   glTranslatef(-20.f, -100.f, -500.f);
  319.   glCallList(CONE);
  320.   glPopMatrix();
  321.  
  322.   if (stencil) {
  323.     glEnable(GL_STENCIL_TEST);
  324.     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  325.     glStencilFunc(GL_ALWAYS, 1, 1);
  326.     glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  327.     glPixelTransferf(GL_DEPTH_BIAS, depthbias);
  328.  
  329.     glDrawPixels(wid, ht, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
  330.  
  331.     glPixelTransferf(GL_DEPTH_BIAS, 0.f);
  332.     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  333.     glStencilFunc(GL_EQUAL, 1, 1);
  334.     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  335.     glDisable(GL_DEPTH_TEST);
  336.  
  337.     glDrawPixels(wid, ht, GL_RGB, GL_FLOAT, color);
  338.  
  339.     glEnable(GL_DEPTH_TEST);
  340.     glDisable(GL_STENCIL_TEST);
  341.   } else
  342.     glDrawPixels(wid, ht, GL_RGB, GL_FLOAT, color);
  343.  
  344.   glutSwapBuffers();
  345. }
  346.  
  347. const int TEXDIM = 256;
  348.  
  349. /* Parse arguments, and set up interface between OpenGL and window system */
  350. int
  351. main(int argc, char *argv[])
  352. {
  353.   GLfloat *tex;
  354.   static GLfloat lightpos[] =
  355.   {50.f, 50.f, -320.f, 1.f};
  356.   static GLfloat sphere_mat[] =
  357.   {1.f, .5f, 0.f, 1.f};
  358.   static GLfloat cone_mat[] =
  359.   {0.f, .5f, 1.f, 1.f};
  360.   GLUquadricObj *obj;
  361.  
  362.   glutInit(&argc, argv);
  363.   glutInitWindowSize(WINDIM, WINDIM);
  364.  
  365.   glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL | GLUT_DOUBLE);
  366.   (void) glutCreateWindow("compositing images with depth");
  367.   glutDisplayFunc(redraw);
  368.   glutKeyboardFunc(key);
  369.   glutMouseFunc(mouse);
  370.   glutMotionFunc(motion);
  371.   glutReshapeFunc(reshape);
  372.  
  373.   /* draw a perspective scene */
  374.   glMatrixMode(GL_PROJECTION);
  375.   glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, FRUSTNEAR, FRUSTFAR);
  376.   glMatrixMode(GL_MODELVIEW);
  377.  
  378.   /* turn on features */
  379.   glEnable(GL_DEPTH_TEST);
  380.   glEnable(GL_LIGHTING);
  381.   glEnable(GL_LIGHT0);
  382.  
  383.   /* place light 0 in the right place */
  384.   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  385.  
  386.   /* remove back faces to speed things up */
  387.   glCullFace(GL_BACK);
  388.  
  389.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  390.  
  391.   glNewList(SPHERE, GL_COMPILE);
  392.   /* make display lists for sphere and cone; for efficiency */
  393.   obj = gluNewQuadric();
  394.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
  395.   gluSphere(obj, 20.f, 20, 20);
  396.   glEndList();
  397.  
  398.   glNewList(CONE, GL_COMPILE);
  399.   glRotatef(-90.f, 1.f, 0.f, 0.f);
  400.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
  401.   gluQuadricOrientation(obj, GLU_INSIDE);
  402.   gluDisk(obj, 0., 20., 20, 1);
  403.   gluQuadricOrientation(obj, GLU_OUTSIDE);
  404.   gluCylinder(obj, 20., 0., 60., 20, 20);
  405.   glEndList();
  406.  
  407.   gluDeleteQuadric(obj);
  408.  
  409.   /* load pattern for current 2d texture */
  410.   tex = make_texture(TEXDIM, TEXDIM);
  411.   glTexImage2D(GL_TEXTURE_2D, 0, 1, TEXDIM, TEXDIM, 0, GL_RED, GL_FLOAT, tex);
  412.   free(tex);
  413.  
  414.   /* storage for saved image */
  415.   color = 0;
  416.   depth = 0;
  417.  
  418.   glReadBuffer(GL_FRONT);  /* so glReadPixel() always get the right image */
  419.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  420.   glPixelStorei(GL_PACK_ALIGNMENT, 1);
  421.  
  422.   glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
  423.   if (glutLayerGet(GLUT_OVERLAY_POSSIBLE)) {
  424.     glutEstablishOverlay();
  425.     glutHideOverlay();
  426.     transparent = glutLayerGet(GLUT_TRANSPARENT_INDEX);
  427.     glClearIndex(transparent);
  428.     red = (transparent + 1) % glutGet(GLUT_WINDOW_COLORMAP_SIZE);
  429.     glutSetColor(red, 1.0, 0.0, 0.0);  /* Red. */
  430.     glutOverlayDisplayFunc(overlay);
  431.     overlaySupport = 1;
  432.   } else {
  433.     printf("Running without overlay rubber banding support.\n");
  434.   }
  435.   glutMainLoop();
  436.   return 0;             /* ANSI C requires main to return int. */
  437. }
  438.